package com.it.reggie.controller;

import com.it.reggie.commen.R;
import io.minio.*;
import io.minio.errors.MinioException;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.KeyGenerator;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

//@RestController
//@RequestMapping("/common")
public class MinioController {


    @PostMapping("/upload")
    public R upLoad(MultipartFile file, HttpServletResponse response) {

        String originalFilename = file.getOriginalFilename();
        String fileName = originalFilename.substring(originalFilename.lastIndexOf("."));
        fileName = UUID.randomUUID().toString() + fileName;
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://127.0.0.1:9000")
                            .credentials("minioadmin", "minioadmin")
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    false;
            try {
                found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("huike-crm").build());
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("huike-crm").build());
            } else {
                System.out.println("Bucket 'huike-crm' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .object(fileName)
                            .bucket("huike-crm")
                            .filename("D:\\img\\"+originalFilename)
                            .build());

//            InputStream inputStream = file.getInputStream();
//            minioClient.putObject(
//                    PutObjectArgs.builder()
//                            .bucket("huike-crm")
//                            .object("fileName")
//                            .contentType("contentType")
//                            .stream(inputStream, inputStream.available(), -1)
//                            .build());

        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        return R.success(fileName);

    }


    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        try {
            /* play.min.io for test and development. */
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://127.0.0.1:9000")
                            .credentials("minioadmin", "minioadmin")
                            .build();

            /* Amazon S3: */
            // MinioClient minioClient =
            //     MinioClient.builder()
            //         .endpoint("https://s3.amazonaws.com")
            //         .credentials("YOUR-ACCESSKEY", "YOUR-SECRETACCESSKEY")
            //         .build();

            {
                // Download 'my-objectname' from 'my-bucketname' to 'my-filename'
                System.out.println("my-objectname is successfully downloaded to my-filename");
                GetObjectResponse object = minioClient.getObject(new GetObjectArgs(DownloadObjectArgs.builder()
                        .bucket("huike-crm")
                        .object(name)
                        .build()));
                IOUtils.copy(object, response.getOutputStream());
            }

        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }


    }

}